//
//  ZCHarmprofileVC.m
//  zctx
//
//  Created by apple on 15/12/10.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCHarmprofileVC.h"
#import "ZCHarmprofileProjectCell.h"
@interface ZCHarmprofileVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataArray;
@end

@implementation ZCHarmprofileVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCHarmprofileVC"];
    [MTA trackPageViewBegin:@"ZCHarmprofileVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCHarmprofileVC"];
    [MTA trackPageViewEnd:@"ZCHarmprofileVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"危害简介";
    [self initnavegation];
    self.dataArray = [NSArray array];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self initrequst];
    });

    //添加tableview
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0,0));
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCHarmprofileProjectCell" bundle:nil] forCellReuseIdentifier:@"HarmCell"];

}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)initrequst{
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [NetworkManager requestFoundingGetOnepackagesWithId:self.pid Mid:UserModel.mid password:UserModel.password success:^(NSArray *dictResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        self.dataArray= [dictResponse copy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
    }];
}


#pragma mark - UITableView DataSource & Delegate


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"HarmCell";
    ZCHarmprofileProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section];
    NSString *content = [NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];
    NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
    NSString *information = [NSString stringWithFormat:@"%@",[dict objectForKey:@"information"]];

    [cell.titleLable setText:title];
    [cell.contentLable setText:content];
    [cell.infoLable setText:information];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier = @"HarmCell";
    ZCHarmprofileProjectCell *cell = [self.tableView dequeueReusableCellWithIdentifier:  reuseIdentifier];
    
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section];
    NSString *content = [NSString stringWithFormat:@"%@",[dict objectForKey:@"content"]];
    NSString *title = [NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]];
    NSString *information = [NSString stringWithFormat:@"%@",[dict objectForKey:@"information"]];
    
    [cell.titleLable setText:title];
    [cell.contentLable setText:content];
    [cell.infoLable setText:information];
    
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    // 得到cell的contentView需要的真实高度
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    NSLog(@"heithy = %f",height);
    // 要为cell的分割线加上额外的1pt高度。因为分隔线是被加在cell底边和contentView底边之间的。
    height += 1.0f;
    
    return height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.00001;
    }else{
        return 6;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}


#pragma mark - instance

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
